﻿using System;
using Csla.Xaml;
using Accounting.Library;
using System.ComponentModel;
using Utility;
using Accounting.Library.Utility;



namespace csla4Skeleton.UI.SL.NghiepVu.Kho.K_PhieuChuyenKho
{
    public class K_PhieuChuyenKho_ViewModel : ViewModel<Accounting.Library.K_PhieuChuyenKho>
    {
        #region Event Declare
        public delegate void ChildList_AddedNew_EventHandler(object sender, ChildListAddedNewArgs e);
       
        public delegate void HangHoaIdChanged_EventHandler(object sender, HangHoaIdChangedEventArgs e);
       
        public event ChildList_AddedNew_EventHandler ChildList_AddedNew;

        public event EventHandler TCHangHoaChanged;
       
        public event HangHoaIdChanged_EventHandler hangHoaIdChanged;
 
        #endregion

        #region Refresh Model
        public K_PhieuChuyenKho_ViewModel()
        {
            BeginRefresh("NewK_PhieuChuyenKho");
            
        }

        public K_PhieuChuyenKho_ViewModel(int Khoa)
        {
            BeginRefresh("GetK_PhieuChuyenKho", Khoa);
           
        }

        public void NewK_PhieuChuyenKhoViewModel()
        {
            BeginRefresh("NewK_PhieuChuyenKho");
            
        }

        public void GetK_PhieuChuyenKhoViewModel(int Khoa)
        {
            BeginRefresh("GetK_PhieuChuyenKho", Khoa);
        }

       
        public void SaveAndNew()
        {
            BeginSave();
            NewK_PhieuChuyenKhoViewModel();
        }
        protected override void OnRefreshed()
        {
            base.OnRefreshed();
            if (Model != null)
            {
                Model.K_PhieuChuyenKho_ChiTiets.AddedNew += new EventHandler<Csla.Core.AddedNewEventArgs<K_PhieuChuyenKho_ChiTietChild>>(K_PhieuChuyenKho_ChiTiets_AddedNew);
               
                Model.TinhChatHangHoaChanged += new EventHandler(OnTinhChatHangHoaChanged);
                Model.HangHoaIdChanged += new EventHandler<HangHoaIdChangedEventArgs>(OnHangHoaIdChanged);
               
            }
        }
        #endregion
        
        #region Add K_PhieuChuyenKho Details Item
        public void AddK_PhieuChuyenKho_ChiTietItem()
        {
            Model.K_PhieuChuyenKho_ChiTiets.AddNewItem(Model.Khoa);
        }

       

        void K_PhieuChuyenKho_ChiTiets_AddedNew(object sender, Csla.Core.AddedNewEventArgs<K_PhieuChuyenKho_ChiTietChild> e)
        {
            OnChildList_AddedNew(sender, new ChildListAddedNewArgs("K_PhieuChuyenKho_ChiTiets"));
        }      

       

        protected virtual void OnChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (ChildList_AddedNew != null)
            {
                //Invokes the delegates.
                ChildList_AddedNew(sender, e);
            }
        }

      
        public void OnHangHoaIdChanged(object sender, HangHoaIdChangedEventArgs e)
        {
            if (hangHoaIdChanged != null)
            {
                hangHoaIdChanged(sender, e);
            }

        }
        public void OnTinhChatHangHoaChanged(object sender, EventArgs e)
        {
            if (TCHangHoaChanged != null)
            {
                TCHangHoaChanged(sender, e);
            }

        }
       
        #endregion

        #region Delete K_PhieuChuyenKho Details Item

        public void DeleteK_PhieuChuyenKho_ChiTietItem(int Index)
        {
            Model.K_PhieuChuyenKho_ChiTiets.DeleteItem(Index);
        }

        #endregion

        #region Nghiep Vu So
        internal void GhiSo()
        {
            this.Model.SaveMode = BusinessConfiguration.Configuration.GhiSoSaveMode;
            BeginSave();
        }

        internal void BoGhi()
        {
            int loaiChungTuId = BusinessConfiguration.Configuration.LoaiChungTuId_K_PhieuChuyenKho;
            int chungTuGocId = Model.Khoa;

            this.IsBusy = true;
            Accounting.Library.GhiSo.BoGhiService.BoGhi(loaiChungTuId, chungTuGocId, (o, ex) =>
            {
                if (ex.Error != null)
                    this.Model.ErrorMessage = ex.Error.Message;
                else
                    this.Model.ErrorMessage = ((Accounting.Library.GhiSo.BoGhiService)ex.Object).ErrorMessage;
                BeginRefresh("GetK_PhieuChuyenKho", Model.Khoa);
            });
            this.IsBusy = false;
        }
#endregion
    }
}
